package com.yoomiito.app.adapter.cicle;

import android.app.Activity;
import android.net.Uri;
import androidx.annotation.Nullable;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scrat.app.selectorlibrary.activity.ImagePreviewActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.cicle.CicleRecommendInfo_1;
import com.yoomiito.app.model.cicle.ImageItemBean;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.CircleImageView;
import com.yoomiito.app.widget.CouponView;
import com.yoomiito.app.widget.RoundImageView;
import com.yoomiito.app.widget.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 新好单库商品使用
 */
public class CicleGoodRemmendAdapter_1 extends BaseQuickAdapter<CicleRecommendInfo_1, BaseViewHolder> {
    public CicleGoodRemmendAdapter_1(@Nullable List<CicleRecommendInfo_1> data) {
        super(R.layout.item_cicle_1, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, CicleRecommendInfo_1 item) {
        helper.addOnClickListener(R.id.tv_share);
        helper.setGone(R.id.awardView,false);
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.goods_default);
        if (!TextUtils.isEmpty(item.getAvatar())) {
            Glide.with(App.getApplication()).load(Uri.parse(item.getAvatar())).apply(requestOptions).into((CircleImageView) helper.getView(R.id.iv_photo));
        }

        ((TextView) helper.getView(R.id.tv_name)).setText(item.getAuthor());
        ((TextView) helper.getView(R.id.tv_date)).setText("" + item.getCtime());
        TextView contentTv = helper.getView(R.id.tv_content);
        contentTv.setText("" + item.getContent());
        contentTv.setOnClickListener(v -> {
            AllUtils.copyText(contentTv.getContext(),item.getContent());
            YMToast.showText("复制成功");
        });

        List<ImageItemBean> imageBeanList = new ArrayList<>();
        if (item.getContent_img() != null && item.getContent_img().size() > 0) {
            for (String url : item.getContent_img()) {
                imageBeanList.add(new ImageItemBean(url,null));
            }
            ImageAdapter adapter = new ImageAdapter(imageBeanList);
            adapter.setOnItemChildClickListener((adapter1, view, position) -> {
//                getOnItemChildClickListener().onItemChildClick(adapter1,view,helper.getLayoutPosition());
                ImagePreviewActivity.show((Activity) mContext, 0, imageListToArray(imageBeanList),position);
            });
            RecyclerView recyclerView = (RecyclerView) helper.getView(R.id.rv_photo);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, Constant.GRID_IMAGE_NUM));
            if (recyclerView.getItemDecorationCount() >0){//避免重复添加itemDecoration
                RecyclerView.ItemDecoration itemDecoration = recyclerView.getItemDecorationAt(0);
                if(itemDecoration == null){
                    recyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dimen_6dp)));
                }
            }else{
                recyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dimen_6dp)));
            }

            recyclerView.setAdapter(adapter);
        }
        LinearLayout goodsLayout = helper.getView(R.id.item_goods_3);
        if (SpannableUtils.isEmptyOr0(item.getProduct_id())){
            goodsLayout.setVisibility(View.GONE);
        }else {
            goodsLayout.setVisibility(View.VISIBLE);
            helper.addOnClickListener(R.id.item_goods_3);
            RoundImageView goodsIv = helper.getView(R.id.goods_iv);
            ImageLoader.getInstance().into(goodsIv.getContext(),item.getItempic(),goodsIv,R.drawable.goods_default);
            helper.setText(R.id.goods_title,item.getTitle());
//            LinearLayout couponLl= helper.getView(R.id.item_coupon_3);
            CouponView couponView = helper.getView(R.id.couponView);
            if (SpannableUtils.isEmptyOr0(item.getCoupon_price())){
                couponView.setVisibility(View.GONE);
            }else {
                couponView.setVisibility(View.VISIBLE);
//                TextView couponTv = helper.getView(R.id.tv_voucher);
                couponView.setText("¥"+item.getCoupon_price());
            }
            helper.setText(R.id.goods_price,item.getPrice());
            ImageView iv = helper.getView(R.id.goods_source);
            if (1==item.getShoptype()){
                iv.setImageResource(R.mipmap.tmall);
            }else {
                iv.setImageResource(R.mipmap.tb);
            }
        }
    }

    private ArrayList<String> imageListToArray(List<ImageItemBean> imageItemBeans){
        ArrayList<String> paths = new ArrayList<>();
        for (int i = 0;i< imageItemBeans.size();i++){
            paths.add(imageItemBeans.get(i).getUrl());
        }
        return paths;
    }
}
